/** 商品 */

var type1;
var type2;
var type3;

function firstChange(theSelect){
	var typeId = theSelect.value;

	if (!type2){
		$.ajax({
			type:"post",
			async:false,
			url: ctx + "/seller/get-types",
			data : {level:2,isZiYing:true},
			success : function(msg){
				type2 = msg;
			}
			});
		}
	var options = "<option value='-1'>--中类--</option>";
	for (var i = 0; i < type2.length; i++){
		if (typeId == type2[i].cBelong){
			var aOption = "<option value='"+type2[i].cCode+"' >"+type2[i].cName+"</option>"
			options += aOption; 
		}
	}
	$("#type2").html(options);
}

function secondClick(theSelect){
	
	var typeId = $("#type1").val();
	if (!type2){
		$.ajax({
			type:"post",
			async:false,
			url: ctx +"/seller/get-types",
			data : {level:2,isZiYing:true},
			success : function(msg){
				type2 = msg;
			}
			});
		
		var options = "<option value='-1'>--中类--</option>";
		for (var i = 0; i < type2.length; i++){
			if (typeId == type2[i].cBelong){
				var aOption = "<option value='"+type2[i].cCode+"' >"+type2[i].cName+"</option>"
				options += aOption; 
			}
		}
		$("#type2").html(options);
		}
}

function ciytChange(citySelect){
	$("#endType").val('');//初始化最终要提交的类型
	//$("#type").html("<option value='-1'>--所属品类--</option>");
	//$("#type2-con").hide();
	$("#type")
	if (!allShops){
		$.ajax({
			type:"post",
			async:false,
			url: ctx + "/seller/get-all-shops",
			data : {isZiYing:true},
			success : function(msg){
				allShops = msg;
			}
			});
	}
	

	var options = "<option value='-1'>--所属商铺--</option>";
	for (var i = 0; i < allShops.length; i++){
		if (citySelect.value == allShops[i].c_city_id){
			var aOption = "<option value='"+allShops[i].cId+"' data-type='"+allShops[i].cType+"' >"+allShops[i].cSname+"</option>"
			options += aOption; 
		}
	}
	$("#shops").html(options);
}

function secondChange(theSelect){
	var typeId = theSelect.value;

	if (!type3){
		$.ajax({
			type:"post",
			async:false,
			url: ctx + "/seller/get-types",
			data : {level:3,isZiYing:true},
			success : function(msg){
				type3 = msg;
			}
			});
		}
	var options = "<option value='-1'>--小类--</option>";
	for (var i = 0; i < type3.length; i++){
		if (typeId == type3[i].cBelong){
			var aOption = "<option value='"+type3[i].cCode+"' >"+type3[i].cName+"</option>"
			options += aOption; 
		}
	}
	$("#type3").html(options);
}

function thirdClick(theSelect){
	
	var typeId = $("#type2").val();
	if (!type3 && typeId != -1){
		$.ajax({
			type:"post",
			async:false,
			url: ctx + "/seller/get-types",
			data : {level:3,isZiYing:true},
			success : function(msg){
				type3 = msg;
			}
			});
		var options = "<option value='-1'>--小类--</option>";
		for (var i = 0; i < type3.length; i++){
			if (typeId == type3[i].cBelong){
				var aOption = "<option value='"+type3[i].cCode+"' >"+type3[i].cName+"</option>"
				options += aOption; 
			}
		}
		$("#type3").html(options);
		}
	
}
function goods_save(){
	$("#save-btn").click();
}
//保存
function goods_save_submit(){
	
	$("#form-goods-add").Validform({
		tiptype:2,
		ajaxPost:true,
		beforeSubmit:function(form){
	var type1 = $("#type1").val();
			var type2 = $("#type2").val();
			var type3 = $("#type3").val();
			
			var endType;
			if (type3 != -1){
				endType = type3;
			}else if (type2 != -1){
				endType = type2;
			}else if (type1 != -1){
				endType = type1;
			}
			
			if (!endType){
				layer.msg('请选择分类', {icon:5,time:1000});
				$("#type1").focus();
				return false;
			}
			var endType = $("#endType").val(endType);
			
			if($("#products_list").val().length<=0){
				var products_str = "";
				var rules = $(".rule-con"); 
				for (var i = 0; i < rules.length; i++){
					products_str += $(rules[i]).find("input[id*='_mkt']").val() +",";
					products_str += $(rules[i]).find("input[id*='_price']").val()+",";
					products_str += $(rules[i]).find("input[id*='_pdt_spec']").val()+",";
					products_str += $(rules[i]).find("input[id*='_store']").val()+",";
					products_str += $(rules[i]).find("input[id*='_unit']").val()+",";
					products_str += $(rules[i]).find("input[id*='_productId']").val()+";";
					//products_str += $(rules[i]).find("select[id*='_city_id']").val()+";";
				}
				$("#products_list").val(products_str);
			} 
			
		},
		callback:function(form){
			if (form.error == "0000"){
				layer.msg('操作成功', {icon:6,time:1000});
			
				var index = parent.layer.getFrameIndex(window.name);
				if (index){//关闭弹出层
					parent.location.href = ctx +"/seller/goodslist"; 
					parent.location.reload(true);
					parent.layer.close(index);
					
				}else{//单独的页面
					location.href =ctx +"/seller/goodslist"; 
				}
			}else{
				alert("增加失败");
			}
			
		}
	});
}

	//增加商品规格
function addProduct(){
	var aProduct = '<div class="rule-con">'
	 + ''+ (++productSize) +'：市场价&nbsp;&nbsp;<input type="text" name="'+ (productSize) +'_mkt" id="'+ (productSize) +'_mkt" placeholder=" 市场价" value="" class="form-control" style=" width:10%" datatype="*" nullmsg="不能为空" >&nbsp;元&nbsp;&nbsp; '
	 + '销售价&nbsp;&nbsp;<input type="text" name="'+ (productSize) +'_price" id="'+ (productSize) +'_price" placeholder=" 销售价" value="" class="form-control" style=" width:10%"  datatype="*" nullmsg="不能为空">'
	 + '&nbsp;元&nbsp;&nbsp;  '
	 + '规格描述&nbsp;&nbsp;<input type="text" name="'+ (productSize) +'_pdt_spec" id="'+ (productSize) +'_pdt_spec" placeholder=" 示例: 500ML" value="" class="form-control" style=" width:10%"  datatype="*" nullmsg="不能为空">&nbsp;&nbsp;'
	 + '&nbsp;库存&nbsp;&nbsp;<input type="text" name="'+ (productSize) +'_store" id="'+ (productSize) +'_store" placeholder=" 库存" value="" class="form-control" style=" width:5%">&nbsp;&nbsp;'
	 + '单位&nbsp;&nbsp;<input type="text" name="'+ (productSize) +'_unit" id="'+ (productSize) +'_unit" placeholder=" 单位" value="" class="form-control" style=" width:10%"  datatype="*" nullmsg="不能为空">'
	 + '<input type="hidden" name="'+ (productSize) +'_productId" id="'+ (productSize) +'_productId" value="0">' ;
	 aProduct  = aProduct +  '&nbsp;<a style="text-decoration:none" class="btn btn-primary radius" onClick="deleteProductForm(this);" href="javascript:;" title="删除">删除</a>'
	 + '</div>';
	
	$("#productCtn").append(aProduct);
}
/*删除规格 */
function product_disable(obj,id){
	layer.confirm('确认删除？', {
		btn: ['确认','取消'], 
		shade: false
	},
	function(){
		
		$.ajax({
			type : "POST",
			url : ctx + "/seller/products-disable",
			data : "id=" + id,
			error : function() {
				layer.msg('操作失败', {icon:5,time:1000});
			},
			success : function(msg) {
				layer.msg('已删除 ' + msg.information, {icon:6,time:1000});
				$(obj).parent(".rule-con").remove();
			}
		});
	},
	function(){
		layer.msg('未删除', {icon:5,time:1000});
	});	
}

/*删除规格 表单*/
function deleteProductForm(form){
	$(form).parent(".rule-con").remove();
}


	function edittext() {
		$("#eg").addClass("no-padding");
		$(".click2edit").summernote({
			lang : "zh-CN",
			focus : true
		})
	};
	
	//删除已保存到商品详情里的图片 
	function deleteDetailPic(btn, imageId){
		layer.confirm('确认删除？', {
			btn: ['确认','取消'], 
			shade: false
		},
		function(){
			layer.msg('请提交保存', {icon:6,time:1000});
			var deleteFile = "<input type='hidden' name='deletePic' value='"+imageId+"'>";
			$(btn).parent("div").remove();
			$("#form-goods-add").append(deleteFile);
		},
		function(){
			layer.close();
		});	
	}
	
	
	
	
$(document).ready(
	function() {
			var $list_covers = $('#fileList_covers'), $list_resources = $('#fileList_resources'),$list_xqt = $('#fileList_xqt'), ratio = window.devicePixelRatio || 1, thumbnailWidth = 100 * ratio, thumbnailHeight = 100 * ratio, uploader_resources, uploader_covers;

			/*图片上传*/
			uploader_covers = WebUploader
			.create({
				auto : true,
				swf : ctx +'/static/plugins/webuploader/0.1.5/Uploader.swf',
				server : ctx +'/file/uploadPic',
				pick : '#iconbutton',
				accept : {
					title : 'Images',
					extensions : 'gif,jpg,jpeg,bmp,png',
					mimeTypes : 'image/jpg,image/jpeg,image/png'
				}
			});

			uploader_covers
			.on(
				'fileQueued',
				function(file) {
					var $li = $('<div id="' + file.id + '" class="file-item thumbnail">'
						+ '<img>'
						+ '<div class="info">'
						+ file.name
						+ '</div>'
						+ '</div>'), $img = $li
					.find('img');

					$list_covers.html($li);
					uploader_covers.makeThumb(
						file,
						function(error,src) {
							if (error) {
								$img.replaceWith('<span>不能预览</span>');
								return;
							}

							$img.attr('src',src);
						},
						thumbnailWidth,
						thumbnailHeight);
				});

			uploader_covers.on('uploadSuccess', function(file,
				response) {
				$('#' + file.id).addClass('upload-state-done');
				$("#covers").val(response.datas);
			});

			uploader_covers.on('uploadError', function(file) {
				var $li = $('#' + file.id), $error = $li
				.find('div.error');
				if (!$error.length) {
					$error = $('<div class="error"></div>')
					.appendTo($li);
				}

				$error.text('上传失败');
			});

			uploader_covers.on('uploadComplete',
				function(file) {
					$('#' + file.id).find('.progress')
					.remove();
				});

			/*轮播图上传*/
			uploader_resources = WebUploader
			.create({
				auto : true,
				swf : ctx +'/static/plugins/webuploader/0.1.5/Uploader.swf',
				server : ctx +'/file/uploadPic',
				pick : '#filePicker_resources',
				accept : {
					title : 'Images',
					extensions : 'gif,jpg,jpeg,bmp,png',
					mimeTypes : 'image/jpg,image/jpeg,image/png'
				}
			});

			uploader_resources.on(
				'fileQueued',
				function(file) {
					var $li = $('<div id="' + file.id + '" class="file-item thumbnail">'
						+ '<img>'
						+ '<div class="info">'
						+ file.name
						+ '</div>'
						+ '</div>'), $img = $li
					.find('img');

					$list_resources.append($li);
					uploader_resources.makeThumb(
						file,
						function(error,
							src) {
							if (error) {
								$img
								.replaceWith('<span>不能预览</span>');
								return;
							}
							$img.attr('src',src);
						},
						thumbnailWidth,
						thumbnailHeight);
				});

			uploader_resources.on('uploadSuccess', function(
				file, response) {
				$('#' + file.id).addClass('upload-state-done');
				var old_val = $("#image_list").val();			
				$("#image_list").val(
					old_val + "," + response.datas);
			});

			uploader_resources
			.on(
				'uploadError',
				function(file) {
					var $li = $('#' + file.id), $error = $li
					.find('div.error');
					if (!$error.length) {
						$error = $(
							'<div class="error"></div>')
						.appendTo($li);
					}

					$error.text('上传失败');
				});

			uploader_resources.on('uploadComplete', function(
				file) {
				$('#' + file.id).find('.progress').remove();
			});
	
	
	/*详情图上传*/
	uploader_xqt = WebUploader
	.create({
		auto : true,
		swf : ctx+ '/static/plugins/webuploader/0.1.5/Uploader.swf',
		server :  ctx+ '/file/uploadPic',
		pick : '#xqTp_resources1',
		accept : {
			title : 'Images',
			extensions : 'gif,jpg,jpeg,bmp,png',
			mimeTypes : 'image/jpg,image/jpeg,image/png'
		}
	});

	uploader_xqt
	.on(
		'fileQueued',
		function(file) {
			var $li = $('<div id="' + file.id + '" class="file-item thumbnail">'
				+ '<img>'
				+ '<div class="info">'
				+ file.name
				+ '</div>'
				+ '</div>'), $img = $li
			.find('img');

			$list_xqt.append($li);
			uploader_xqt
			.makeThumb(
				file,
				function(error,
					src) {
					if (error) {
						$img
						.replaceWith('<span>不能预览</span>');
						return;
					}
					$img.attr('src',src);
				},
				thumbnailWidth,
				thumbnailHeight);
		});

	uploader_xqt.on('uploadSuccess', function(
		file, response) {
		$('#' + file.id).addClass('upload-state-done');
		var old_val = $("#detail_pics").val();
		$("#detail_pics").val(old_val + "," + response.datas);
	});

	uploader_xqt
	.on(
		'uploadError',
		function(file) {
			var $li = $('#' + file.id), $error = $li
			.find('div.error');
			if (!$error.length) {
				$error = $(
					'<div class="error"></div>')
				.appendTo($li);
			}

			$error.text('上传失败');
		});

	uploader_xqt.on('uploadComplete', function(
		file) {
		$('#' + file.id).find('.progress').remove();
	});
	/*/详情图片上传*/
	
	
});


	
	function goods_p_delete(obj,id){
		layer.confirm('确认删除？', {
			btn: ['确认','取消'], 
			shade: false
		},
		function(){
			$.ajax({
				type : "POST",
				url : ctx +"/seller/t_goods_p-delete",
				data : "id=" + id,
				error : function() {
					layer.msg('操作失败', {icon:5,time:1000});
				},
				success : function(obj, msg) {
					layer.msg('已删除 ' + msg.information, {icon:6,time:1000});
					
					var img_path = $("#image_path_"+id).val();
					var old_del = $("#image_list_del").val();
					$("#image_list_del").val(old_del + ", " + img_path);
					//clear
					$("#image_"+id).html("");
				}
			});
		},
		function(){
			layer.msg('未删除', {icon:5,time:1000});
		});	
}
	
	
	
	
	
	
	